
# coding:utf-8
from base import config
from utils.suger import *
from lib.fluxext import singleton, ViewGroup
from system.game_time import GameTime
from gui.hud import SkillButton, SkillTip, HealBar, ManaBar, BattleInfo, Lock
from gui.logwindow import LogWindow


@singleton
class UIManager(ViewGroup):

    def init(self):
        ''' 解释一下不作为构造器的原因：因为core引用screen，screen引用这里。那么在core加载字体之前这里就被执行了。
            于是这造成了TextView的报错。因此用了这个办法。
        '''
        hp = HealBar()
        mp = ManaBar()

        skl = ViewGroup()
        skl_key = ['Q', 'W', 'E', 'R', 'T']
        for i in range(len(skl_key)):
            skl[i] = SkillButton()
            skl[i].SetSize(36, 36)
            skl[i].SetPos(config.SCREEN_WIDTH/2+(i-2)*(36+3), config.SCREEN_HEIGHT-30)
            skl[i].SetKeyTip(skl_key[i])
        del skl_key

        skltip = SkillTip()
        skltip.SetText('AAABBB')
        skltip.SetVisible(False)

        self.time = GameTime()
        self.time.layer_offset = -1

        self.hp = hp
        self.mp = mp
        self.skl = skl
        self.skltip = skltip

        self.SetHUD(True)
        #self.battleinfo = BattleInfo()

        self.lock = Lock()
        self.lock.layer_offset = -2

        self.log = LogWindow()

        @smokesignal.on('unit_hp_mp_changed')
        def _callback(player):
            if player == Game().on_control:
                self.hp.SetValue(player.attr['hp'], player.attr['hp_max'], player.attr['hp_regain'])
                self.mp.SetValue(player.attr['mp'], player.attr['mp_max'], player.attr['mp_regain'])

        @smokesignal.on('btn_spell_cast')
        def _callback(index):
            if skl[index].skl:
                skl[index].Cast()


ui = UIManager()
